package com.ps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ps.context.BaseContext;
import com.ps.dto.ShoppingCartDTO;
import com.ps.entity.Medicine;
import com.ps.entity.Setmeal;
import com.ps.entity.ShoppingCart;
import com.ps.mapper.MedicineMapper;
import com.ps.mapper.SetmealMapper;
import com.ps.mapper.ShoppingCartMapper;
import com.ps.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    // 注入购物车Mapper
    private final ShoppingCartMapper shoppingCartMapper;
    // 注入药品Mapper
    private final MedicineMapper medicineMapper;
    // 注入药品组合Mapper
    private final SetmealMapper setmealMapper;

    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 创建购物车对象
        ShoppingCart shoppingCart=new ShoppingCart();
        // 将DTO中的属性复制到购物车对象中
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 获取当前用户ID
        Long userId= BaseContext.getCurrentId();
        // 设置购物车对象中的用户ID
        shoppingCart.setUserId(userId);

        // 根据购物车对象查询购物车列表
        List<ShoppingCart> list =  lambdaQuery()
                .eq(userId!=null,ShoppingCart::getUserId,userId)
                .eq(shoppingCart.getMedicineId()!=null,ShoppingCart::getMedicineId,shoppingCart.getMedicineId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId())
                .eq(shoppingCart.getMedicineForm()!=null,ShoppingCart::getMedicineForm,shoppingCart.getMedicineForm())
                .list();
        // 如果购物车列表不为空
        if ( list !=null && !list.isEmpty()) {
            // 获取购物车列表中的第一个元素
            ShoppingCart cart = list.get(0);
            // 将购物车列表中的第一个元素的数量加1
            cart.setNumber(cart.getNumber()+1);
            // 更新购物车列表中的第一个元素的数量
            lambdaUpdate().eq(ShoppingCart::getId,cart.getId())
                    .set(ShoppingCart::getNumber,cart.getNumber()).update();
        }else{
            // 获取药品ID
            Long medicineId = shoppingCartDTO.getMedicineId();
            // 如果药品ID不为空
            if (medicineId != null) {
                //当前为药品
                // 根据药品ID查询药品
                Medicine medicine=medicineMapper.selectById(medicineId);
                // 设置购物车对象中的药品名称
                shoppingCart.setName(medicine.getName());
                // 设置购物车对象中的药品图片
                shoppingCart.setImage(medicine.getImage());
                // 设置购物车对象中的药品价格
                shoppingCart.setAmount(medicine.getPrice());
            }else{
                //当前为药品组合
                // 获取药品组合ID
                Long setmealId = shoppingCartDTO.getSetmealId();
                // 根据药品组合ID查询药品组合
                Setmeal setmeal=setmealMapper.selectById(setmealId);
                // 设置购物车对象中的药品组合名称
                shoppingCart.setName(setmeal.getName());
                // 设置购物车对象中的药品组合图片
                shoppingCart.setImage(setmeal.getImage());
                // 设置购物车对象中的药品组合价格
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 设置购物车对象中的数量为1
            shoppingCart.setNumber(1);
            // 设置购物车对象中的创建时间为当前时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 将购物车对象插入数据库
            save(shoppingCart);
        }
    }



    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart=ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list =lambdaQuery()
                .eq(ShoppingCart::getUserId,shoppingCart.getUserId())
                .list();
        return list;
    }


    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.delete(new QueryWrapper<ShoppingCart>()
                .eq("user_id",userId));
    }

    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list =lambdaQuery()
                .eq(shoppingCart.getUserId()!=null,ShoppingCart::getUserId,shoppingCart.getUserId())
                .eq(shoppingCart.getMedicineId()!=null,ShoppingCart::getMedicineId,shoppingCart.getMedicineId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId())
                .eq(shoppingCart.getMedicineForm()!=null,ShoppingCart::getMedicineForm,shoppingCart.getMedicineForm())
                .list();

        if(list != null && !list.isEmpty()){
            shoppingCart = list.get(0);

            Integer number = shoppingCart.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                lambdaUpdate()
                        .eq(shoppingCart.getId()!=null,ShoppingCart::getId,shoppingCart.getId())
                        .set(shoppingCart.getNumber()!=null,ShoppingCart::getNumber,shoppingCart.getNumber())
                        .update(shoppingCart);
            }
        }
    }
}
